package manage.service;

import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.common.service.Service;
import m.system.cache.CacheUtil;
import m.system.db.DBManager;
import m.system.db.DataRow;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.StringUtil;
import manage.model.AdminGroupPower;
import manage.run.ModuleInitRun;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class AdminGroupPowerService extends Service {
	/**
	 * 判断权限是否包含power
	 * 
	 */
	public boolean hasPower(String admin_oid,String orgOid,String... power) {
		boolean flag=false;
		Map<String,Boolean> powerMap=getPowerMap(admin_oid,orgOid);
		for(String p : power) {
			if(null!=powerMap.get(p)&&powerMap.get(p))
				flag=true;
		}
		return flag;
	}
	/**
	 * 获取用户对应的权限
	 *  admin_oid
	 * 
	 *  SQLException
	 *  MException
	 */
	public Map<String,Boolean> getPowerMap(String admin_oid,String orgOid) {
		List<AdminGroupPower> list=CacheUtil.getList(AdminGroupPower.class,admin_oid+","+StringUtil.noSpace(orgOid));
		Map<String,Boolean> powerMap= new HashMap<>();
		for(AdminGroupPower power : list){
			powerMap.put(power.getName(), true);
		}
		return powerMap;
	}
	public List<String> getPowerTitleList(String admin_oid,String orgOid) throws SQLException, MException{
		List<String> powerList= new ArrayList<>();
		Map<String,Boolean> powerMap=getPowerMap(admin_oid,orgOid);
		List<String[]> plist=ModuleInitRun.getPowerList();
		for(String[] ps : plist){
			Boolean b=powerMap.get(ps[0]);
			if(null!=b&&powerMap.get(ps[0])) {
				powerList.add(ps[1]);
			}
		}
		return powerList;
	}
	/**
	 * 获取用户组对应的权限
	 *  admin_group_oid
	 * 
	 *  SQLException
	 *  MException
	 */
	public Map<String,Boolean> getPowerMapByGroup(String admin_group_oid) throws SQLException, MException{
		Map<String,Boolean> powerMap= new HashMap<>();
		List<AdminGroupPower> list=ModelQueryList.getModelList(AdminGroupPower.class, new String[] {"name"}, null,
			QueryCondition.eq("adminGroup.oid",admin_group_oid)
		);
		for(AdminGroupPower power : list){
			powerMap.put(power.getName(), true);
		}
		return powerMap;
	}
	private String getAdminGroupPowerOid(AdminGroupPower model) throws SQLException{
		DataRow dr=DBManager.queryFirstRow("select oid from os_admin_group_power gm where gm.admin_group_oid=? and gm.name=?",
			model.getAdminGroup().getOid(),model.getName());
		if(null!=dr){
			return dr.get(String.class,"oid");
		}else{
			return null;
		}
	}
	public void addAdminGroupPower(AdminGroupPower model) throws SQLException, MException{
		model.setOid(getAdminGroupPowerOid(model));
		if(StringUtil.isSpace(model.getOid())){
			model.setOid(GenerateID.generatePrimaryKey());
			ModelUpdateUtil.insertModel(model);
		}
		CacheUtil.clear(AdminGroupPower.class);
	}
	public void removeAdminGroupPower(AdminGroupPower model) throws SQLException, MException{
		model.setOid(getAdminGroupPowerOid(model));
		if(model.getOid().length()==1){
			throw new MException(this.getClass(),"初始化权限不能取消!");
		}
		if(!StringUtil.isSpace(model.getOid())){
			ModelUpdateUtil.deleteModel(model);
		}
		CacheUtil.clear(AdminGroupPower.class);
	}
}
